/**
 * 
 * Copyright (C) 2011 Cody Stoutenburg . All rights reserved.
 *
 *       This program is free software; you can redistribute it and/or
 *       modify it under the terms of the GNU Lesser General Public License
 *       as published by the Free Software Foundation; either version 2.1
 *       of the License, or (at your option) any later version.
 *
 *       This program is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *       GNU Lesser General Public License for more details.
 *
 *       You should have received a copy of the GNU Lesser General Public License
 *       along with this program; if not, write to the Free Software
 *       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 * 
 */
package ca.usherbrooke.mainApp;

import jade.content.lang.Codec;
import jade.content.lang.sl.SLCodec;
import jade.content.onto.Ontology;
import jade.content.onto.basic.Action;
import jade.core.AID;
import jade.domain.JADEAgentManagement.JADEManagementOntology;
import jade.domain.JADEAgentManagement.ShutdownPlatform;
import jade.lang.acl.ACLMessage;
import jade.wrapper.AgentController;
import jade.wrapper.ContainerController;
import jade.wrapper.StaleProxyException;

import java.io.File;

import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import ca.usherbrooke.agent.BasicAgent;
import ca.usherbrooke.utils.ExceptionTool;

/**
 * @author Cody Stoutenburg
 * 
 */
public class MainAppAgent extends BasicAgent {
	private static final long serialVersionUID = 1L;

	public static MainFrame APP_FRAME = null;

	/**
	 * This directory contains all user agent class
	 */
	public static final File DIRECTORY_AGENT = new File("./user_agent/");
	/**
	 * This directory contains all user goal class
	 */
	public static final File DIRECTORY_GOAL = new File("./user_goal/");
	/**
	 * This directory contains all simulation
	 */
	public static final File DIRECTORY_SIMULATION = new File(
			"./user_simulation/");

	@Override
	protected void setup() {
		super.setup();
		try {
			// Set cross-platform Java L&F (also called "Metal")
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}

		APP_FRAME = new MainFrame(this);
	}

	public AID startAgent(String name, String className, Object[] args) {
		AID agent = null;
		ContainerController cc = getContainerController();
		try {
			AgentController ag = cc.createNewAgent(name, className, args);
			ag.start();
			agent = new AID(ag.getName(), true);
		} catch (StaleProxyException ex) {
			ExceptionTool.showError("we fail to create the agent " + name, ex);
		}

		return agent;
	}

	public void stopJade() {
		Codec codec = new SLCodec();
		Ontology jmo = JADEManagementOntology.getInstance();
		getContentManager().registerLanguage(codec);
		getContentManager().registerOntology(jmo);
		ACLMessage msg = new ACLMessage(ACLMessage.REQUEST);
		msg.addReceiver(getAMS());
		msg.setLanguage(codec.getName());
		msg.setOntology(jmo.getName());
		try {
			getContentManager().fillContent(msg,
					new Action(getAID(), new ShutdownPlatform()));
			send(msg);
		} catch (Exception e) {
			ExceptionTool.showError("we fail to stop jade", e);
		}
	}
}
